<?php

namespace App\Admin\Controllers;

use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Auth\Database\Role;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use DB;
use App\Models\Casual_xc_res;
use App\Models\Casual_xc_record;
use App\Models\Casual_xc_ques_type;
use App\Models\Department;
use App\Models\Campus;

class Casual_xcController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header("巡场点位");
            $content->description("选择");

            $content->body($this->navigation_view());
        });
    }

    public  function navigation_view()
    {
        //所有校区
        $compus = Casual_xc_res::where('pid','0')->pluck('res_name','id')->toArray();
        foreach ($compus as $rk1 => $rk2) {
            $res1[$rk1] = Casual_xc_res::where('pid',$rk1)->pluck('res_name','id')->toArray();
            foreach ($res1[$rk1] as $rk2 => $rv2) {
                $res2[$rk2] = Casual_xc_res::where('pid',$rk2)->pluck('res_name','id')->toArray();
                    foreach ($res2[$rk2] as $rk3 => $rv3) {
                        $res3[$rk3] = Casual_xc_res::where('pid',$rk3)->pluck('res_name','id')->toArray();
                }
            }

        }
        return view('admin.xunchang.casual_xc_navigation_view',compact('compus','res1','res2','res3'));
    }
    
    public function record_ques(Request $request)
    {
        $res_id = $request['res_id'];
        //dd($res_id);
        $res_full_name = $this->get_res_full_name($res_id);
        $ques_type = Casual_xc_ques_type::pluck('type_name','id')->toArray();
        $department = Department::where('id','>','1')->pluck('name','id')->toArray();
        return Admin::content(function (Content $content) use($res_id,$res_full_name,$ques_type,$department) {

            $content->header($res_full_name);
            $content->description("问题填写");

            $content->body(view('admin.xunchang.casual_xc_record_ques',compact('res_id','res_full_name','ques_type','department')));
        });
    }

    public function get_res_full_name($res_id)
    {
        //上一级点位ID
        $parent_res1 = Casual_xc_res::where('id',$res_id)->pluck('res_name','pid')->toArray();
        //dd($parent_res1);
        //上2级点位ID
        foreach ($parent_res1 as $key1 => $value1) {
            $parent_res2 = Casual_xc_res::where('id',$key1)->pluck('res_name','pid')->toArray();
            //dd($parent_res2);
            if ($key1!=0) {
                foreach ($parent_res2 as $key2 => $value2) {
                    $parent_res3 = Casual_xc_res::where('id',$key2)->pluck('res_name','pid')->toArray();
                    //dd($parent_res3);
                    //如果没到顶，则接着找出上3级点位id
                    if ($key2!=0) { 
                        foreach ($parent_res3 as $key3 => $value3) {
                            $parent_res4 = Casual_xc_res::where('id',$key3)->pluck('res_name','pid')->toArray();
                            //dd($parent_res4);
                            if ($key3!=0) {
                                foreach ($parent_res4 as $key4 => $value4) { 
                                    $res_full_name = $value4.">".$value3.">".$value2.">".$value1;
                                    //dd($res_full_name);
                                }
                            }
                            else{
                                $res_full_name = $value3.">".$value2.">".$value1;
                            }
                        }
                    }else{
                        //到顶
                        $res_full_name = $value2.">".$value1;
                    }
                }
            }else{
                $res_full_name = $value1;
            }  
        }
        return $res_full_name;
    }

    public function casual_xc_save(Request $request)
    {
        $files = $this->upload($request);
        if (is_null($files)) {
            $ques_img = null;
        }else{
            $ques_img = implode(";", $files);
        }
        
        $compus_name = explode(">", $request['res_full_name'])[0];
        if ($compus_name=="七彩校区") {
            $compus = 2;
        }elseif ($compus_name=="C86校区") {
           $compus = 1;
        }else{
	  $compus = 3;
}
        if ($request['is_handled']==1) {
            $is_handled = '1';
        }else{
            $is_handled = '0';
        }
        $data[] = [
            'compus' => $compus,
            'xc_type' => '1',
            'xc_res_id' => $request['res_id'],
            'status' => '0',
            'ques_type_id' => $request['ques_type'],
            'dep_id' => $request['dep'],
            'ques_desc' => $request['ques_desc'],
            'ques_img' => $ques_img,
            'is_handled' => $is_handled,
            'created_at' => date('Y-m-d H:i:s'),
            'xc_user' => Admin::user()->en_name
        ];
       
        Casual_xc_record::insert($data);
    }

    public function upload(Request $request){
        $file = $request->file('my_img');
        if (is_null($file)) {
            $filePath = null;
        }else{
            //dd($file);
            $filePath =[];  // 定义空数组用来存放图片路径
            foreach ($file as $key => $value) {
                // 判断图片上传中是否出错
                if (!$value->isValid()) {
                    exit("上传图片出错，请重试！");
                }
                if(!empty($value)){//此处防止没有多文件上传的情况
                    $allowed_extensions = ["jpg", "jpeg", "gif", "png","bmp"];
                    if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
                        exit('您只能上传PNG、JPG、JPEG、BMP或GIF格式的图片！');
                    }
                    $destinationPath = '/upload/xc_img/'.date('Y-m-d'); // public文件夹下面uploads/xxxx-xx-xx 建文件夹
                    $extension = $value->getClientOriginalExtension();   // 上传文件后缀
                    $fileName = date('YmdHis').mt_rand(100,999).'.'.$extension; // 重命名
                    $value->move(public_path().$destinationPath, $fileName); // 保存图片
                    $filePath[] = $destinationPath.'/'.$fileName; 
                }
            }
            
        }
        // 返回上传图片路径，用于保存到数据库中
        return $filePath;
        
    }
}
